<template>
	<view class="header">
		<view>
			<uni-swipe-action :options="options" v-for="(item,index) in addList" :key='index' @click='handler($event,item)' style='margin-bottom: 20upx;'>
				<view class="content" @click="checked(item.id)">
					<view class="box">
						<uni-icon type="location" size="50" color="#999"></uni-icon>
						<view class="pox">
							<view style="display: flex;justify-content: space-between;">
								<p class="name">{{item.userName}}</p>
								<p v-if='item.isDefault === 1' style='color: #D7000F;'>默认地址</p>
							</view>
							<p class="address">{{item.provinceName}}{{item.cityName}}{{item.countyName}}{{item.detailInfo}}</p>
							<p class="address">{{item.telNumber}}</p>
						</view>
						<uni-icon type="arrowright" size="50"></uni-icon>
					</view>
				</view>
			</uni-swipe-action>
		</view>
		<view style="font-size: 24upx;color:#888;text-align: right;">滑动可进行编辑、删除</view>
		<view class="add-btn">
			<button type="primary" class="login-btn" @click="toaddAddress()">增加收货地址</button>
		</view>
	</view>
</template>

<script>
    import {
        get,
        post,
        showToast
    } from '../../js/common';
    export default {
        data() {
            return {
                userId: uni.getStorageSync('userId'),
                addList: {},
                mark:null,
				options: [{
					text: '编辑',
					style: {
						backgroundColor: '#007AFF'
					}
				},{
					text: '删除',
					style: {
						backgroundColor: '#D7000F'
					}
				}],
				ischecked:false
            }
        },
		onLoad(e) {
			this.ischecked = e.choose
		},
        onShow() {
            this.getAddList()
        },
        methods: {
            getAddList() {
                let params = {
                    id: this.userId
                }
                post('mobileapi/mobile/member/usercenter/showUserAddress', params).then(res => {
                    this.addList = res.address
            })
            },
			handler(e,item){
				if(e.index === 0){
					this.toedit(item)
				}else{
					this.handleDel(item)
				}
			},
            // 删除
            handleDel(item) {
                let params = {
                    id: item.id
                }
                post('mobileapi/mobile/member/usercenter/deleteAddress', params).then(res => {
                    uni.showToast({
                    title: '删除成功',
                    icon: 'success'
                })
                this.getAddList()
            })
            },
            toedit(item) {
                uni.navigateTo({
                    url: "/pages/address/editAddress?name=" + item.userName + '&telNumber=' + item.telNumber + '&provinceName=' +
                    item.provinceName + '&cityName=' + item.cityName + '&countyName=' + item.countyName + '&detailInfo=' + item.detailInfo +
                    '&isDefault=' + item.isDefault + '&id=' + item.id
                })
            },
            toaddAddress() {
                uni.navigateTo({
                    url: "/pages/address/addAddress"
                })
            },
			checked(id){
				if(this.ischecked){
					uni.setStorageSync('checkAdd', id)
					uni.navigateBack(1)
				}
			}
        }
    };
</script>

<style>
	.header {
		font-size: 30upx;
		padding: 20upx;
	}

	.content {
		display: flex;
	}

	.box {
		padding: 20upx;
		width: 100%;
		height: 180upx;
		display: flex;
		font-size: 28upx;
		justify-content: space-between;
		background-color: #FFFFFF;
		border-radius: 6upx;
		align-items: Center;
	}

	.link {
		display: flex;
	}

	.pox {
		flex: 1;
		margin: 0 20upx;
	}

	.address {
		color: #666;
		margin-top: 10upx;
	}

	.add-btn {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
	}
	
	.login-btn{
		background: #D7000F;
		font-size: 30upx;
		border-radius: 0;
	}
</style>
